import java.util.Arrays;

public class LeetCode475 {
    public static void main(String[] args) {
        System.out.println(
                findRadius(
                        new int[]{1, 5}, new int[]{2}
                )
        );
    }


    public static int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);

        int maxRadius = Integer.MIN_VALUE;
        int currentIndex = 0;
        for (int house : houses) {
            int gtHeat = heaters.length - 1;
            for (int i = currentIndex; i < heaters.length; i++) {
                if (heaters[i] > house) {
                    gtHeat = i;
                    currentIndex = i;
                    break;
                }
            }

            int minLength;
            if (gtHeat == 0) {
                minLength = Math.abs(house - heaters[0]);
            } else {
                minLength = Math.min(Math.abs(heaters[gtHeat] - house), Math.abs(heaters[gtHeat - 1] - house));
            }

            if (minLength > maxRadius) {
                maxRadius = minLength;
            }
        }

        return maxRadius;
    }
}
